package com.suineg.pms.commons.wabacus;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suineg.pms.commons.AppConfig;
import com.suineg.pms.entity.base.Barn;
import com.suineg.pms.entity.base.Building;
import com.suineg.pms.service.base.BarnService;
import com.suineg.pms.service.base.BuildingService;
import com.wabacus.config.component.application.report.ReportBean;
import com.wabacus.system.ReportRequest;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditActionBean;
import com.wabacus.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wabacus.system.intercept.AbsInterceptorDefaultAdapter;
import com.wabacus.util.Consts;

/**
 * 车库的保存拦截器
 *@author Suine
 */
public class BarnInterceper extends AbsInterceptorDefaultAdapter{
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	BuildingService buildingService = AppConfig.getInstance().getWac().getBean(BuildingService.class);
	BarnService barnService = AppConfig.getInstance().getWac().getBean(BarnService.class);

	@Override
	public int doSavePerAction(ReportRequest rrequest, ReportBean rbean, Map<String, String> mRowData, Map<String, String> mParamValues, AbsEditActionBean actionbean,
			AbsEditableReportEditDataBean editbean) {
		// TODO Auto-generated method stub
		
		check(rrequest, mRowData);
		return super.doSavePerAction(rrequest, rbean, mRowData, mParamValues, actionbean, editbean);
	}
	
	public void check(ReportRequest rrequest, Map<String, String> mRowData){
		String buildingID = mRowData.get(Messages.getString("BarnInterceper.0")); //$NON-NLS-1$
		String no = mRowData.get(Messages.getString("BarnInterceper.1")); //$NON-NLS-1$
		String location = mRowData.get(Messages.getString("BarnInterceper.2")); //$NON-NLS-1$
		String id = mRowData.get(Messages.getString("BarnInterceper.3")); //$NON-NLS-1$
		String communtityId = mRowData.get(Messages.getString("BarnInterceper.4")); //$NON-NLS-1$
		
		
		
		Building building = buildingService.get(Long.valueOf(buildingID));
		List<Barn> barnList = building.getBarnList();
		Barn barn = null;
		
		if(id == null || id.isEmpty()){			//新加
			barn = barnService.findByNo(no);
			if(barn != null){
				rrequest.getWResponse().getMessageCollector().warn(Messages.getString("BarnInterceper.5"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
				return ;
			}
			
			if(barnList != null && !barnList.isEmpty()){
				for(Barn b : barnList){
					if(b.getLocation().equals(location)){
						rrequest.getWResponse().getMessageCollector().warn(Messages.getString("BarnInterceper.6"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
						return ;
					}
				}
			}
		}else{									//修改
			
			barn = barnService.get(Long.valueOf(id));
			if(!barn.getNo().equals(no)){			//没有修改编号 
				Barn n = barnService.findByNo(no);
				if(n != null){
					rrequest.getWResponse().getMessageCollector().warn(Messages.getString("BarnInterceper.7"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
					return ;
				}
			}
			if(!barn.getLocation().equals(location)){
				check1(rrequest, buildingID, location);
			}
			if(!barn.getBuilding().getId().equals(Long.valueOf(buildingID))){
				check1(rrequest, buildingID, location);
			}
			if(!barn.getCommunity().getId().equals(Long.valueOf(communtityId))){
				check1(rrequest, buildingID, location);
			}
		}

		
		
	}
	
	public void check1(ReportRequest rrequest,String buildingId,String location){
		List<Barn> barnList = null;
		barnList = buildingService.get(Long.valueOf(buildingId)).getBarnList();
		for(Barn n1 : barnList){
			if(n1.getLocation().equals(location)){
				rrequest.getWResponse().getMessageCollector().warn(Messages.getString("BarnInterceper.8"), false, Consts.STATECODE_FAILED); //$NON-NLS-1$
				return ;
			}
		}
	}

}

